import Vue from 'vue'
import Router from 'vue-router'
Vue.use(Router)
const originalPush = Router.prototype.push
Router.prototype.push = function push(location) {
  return originalPush.call(this, location).catch((err) => err)
}
import Layout from '@/layout'

export const constantRoutes = [
  {
    path: '',
    component: Layout,
    redirect: 'index',
    children: [
      {
        path: '/index',
        component: (resolve) => require(['../views/index.vue'], resolve),
        name: '首页',
        meta: {
          title: '控制台',
          icon: 'fa fa-desktop',
          noCache: true,
          affix: true,
        },
      },
    ],
  },

  {
    path: '',
    component: Layout,
    redirect: 'live/list',
  },
  {
    path: '/login',
    component: (resolve) => require(['@/views/login'], resolve),
    hidden: true,
  },
  {
    path: '/404',
    component: (resolve) => require(['@/views/error/404'], resolve),
    hidden: true,
  },
  {
    path: '/redirect',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/redirect/:path(.*)',
        component: (resolve) => require(['@/views/redirect'], resolve),
      },
    ],
  },
  {
    path: '/gen',
    component: Layout,
    hidden: true,
    children: [
      {
        path: 'edit/:tableId',
        component: (resolve) =>
          require(['@/views/system/tool/gen/edit'], resolve),
        name: 'GenEdit',
        meta: { title: '修改生成配置' },
      },
    ],
  },
  {
    path: '/live/data',
    component: (resolve) => require(['@/views/live/screen/data.vue'], resolve),
  },
  {
    path: '/live/control',
    component: (resolve) =>
      require(['@/views/live/screen/control.vue'], resolve),
  },
  {
    path: '/live/action',
    component: (resolve) =>
      require(['@/views/live/action/action.vue'], resolve),
  },
  {
    path: '/live/infolist',
    component: (resolve) =>
      require(['@/views/live/screen/infolist.vue'], resolve),
  },
  {
    path: '/live/play',
    component: (resolve) => require(['@/views/live/screen/play.vue'], resolve),
  },
  {
    path: '/agreement/privacy',
    component: (resolve) => require(['@/views/agreement/privacy.vue'], resolve),
  },
  {
    path: '/agreement',
    component: (resolve) => require(['@/views/agreement/user.vue'], resolve),
  },
    
]

// const createRouter = () => new Router({
//   mode: 'hash',
//   routes: constantRoutes
// })
// const router = createRouter()
// export function resetRouter () {
//     const newRouter = createRouter()
//     router.matcher = newRouter.matcher // the relevant part
//   }

//   export default router
const createRouter = () =>
  new Router({
    // mode: 'history', // require service support
    scrollBehavior: () => ({ y: 0 }),
    routes: constantRoutes,
  })

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
